#!D:/CODE/python
# -*- coding: utf-8 -*-
# @Time : 2020/5/22 23:09
# @Author : Alexdong
# @Site : 
# @File : 多线程共享全局变量.py
# @Software: PyCharm
# Functional description: 不同线程都会共享全局变量
import time
import threading
g_num = 100

def test1():
    global g_num
    mutex.acquire()  # 上锁
    for i in range(5):
        g_num += 1
    mutex.release()
    print("test1:的g_num:%d" % g_num)

def test2():
    for i in range(10):
        mutex.acquire()
        g_num += 1
        mutex.release()
    print("test2:的g_num:%d" % g_num)

# 创建一个互斥锁，默认没有上锁
mutex  = threading.Lock()



def main():
    t1 = threading.Thread(target=test1)
    t2 = threading.Thread(target=test2)

    t1.start()
    time.sleep(1)
    t2.start()
    time.sleep(1)

    print("main的g_num:%d" % g_num)



if __name__ =="__main()__":
    main()